<template>
  <el-container>
  <el-header>
    <div>
      <img src="../assets/logo.png"/>
      <span>商城管理系统</span>
    </div>
    <el-button type="primary" @click="logout">退出</el-button>
  </el-header>
  <el-container>
    <el-aside :width="isCollapse ? '64px' : '200px'">
      <div class="toggleCollapse" @click="toggleCollapse">|||</div>
      <el-menu
      class="el-menu-vertical-demo"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#409eff"
      unique-opened
      :collapse="isCollapse"
      :collapse-transition="false"
      router
      :default-active="activePath">
      <el-submenu :index="item.id + ''" :key="item.id" v-for="item in menuList">
        <template slot="title">
          <i :class="menuObject[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item :index="'/' + subItem.path"
        :key="subItem.id" v-for="subItem in item.children"
        @click="saveNavPath('/' + subItem.path)">
           <i class="el-icon-collection-tag"></i>
          <span> {{subItem.authName}}</span>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  created () {
    // 加载前调用
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      this.$router.push('/login')
      window.sessionStorage.clear()
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavPath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  data () {
    return {
      menuList: [],
      menuObject: {
        '125': 'el-icon-user',
        '103': 'el-icon-key',
        '101': 'el-icon-shopping-cart-2',
        '102': 'el-icon-s-order',
        '145': 'el-icon-s-data'
      },
      isCollapse: false,
      activePath: ''
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  padding-left: 0px;
  background-color: skyblue;
  display: flex;
  justify-content: space-between;
  div {
    display: flex;
    width: 100%;
    height: 60px;
    line-height: 60px;
    img {
      height: 100%;
    }
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #545c64;
  > .el-menu {
    border-right: none;
  }
}
.el-container {
  height: 100%;
}
.toggleCollapse {
  background-color: #545c64;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  height: 24px;
  color: #fff;
  cursor: pointer;
}
</style>
